<?php
/**
 *
 * @package php.ui.winbinder.win32
 */

/**
 * WBUser
 * 
 * @author easylogic
 *
 */
class WBUser extends WBLibrary { 

	const WM_COMMAND = 273;
	const BN_CLICKED = 0;
	
	const SW_HIDE = 0;
	const SW_SHOW = 1;
	
	const IDC_ARROW = 32512;
	const IDC_HAND = 32649;
	const IDC_CROSS = 32515;
	const IDC_SIZEWE = 32644;
	const IDC_SIZENS = 32645;
	const IDC_SIZENWSE = 32642;
	const IDC_SIZENESW = 32643;
	const IDC_SIZE = 32640;	
	
	const BM_SETCHECK 			= 241;
	
	const LVM_FIRST 			= 0x1000;
	const LVM_DELETEALLITEMS 	= 0x1009;
	const LVM_GETITEMCOUNT 		= 0x1004;
	const LVM_GETITEMSTATE 		= 0x102C;
	const LVM_GETSELECTEDCOUNT 	= 0x1032;	
	
	const LVIS_SELECTED			= 2;
	
	const TCM_GETCURSEL			= 4875;
	const CB_FINDSTRINGEXACT 	= 344;
	const CB_SETCURSEL 			= 334;
	const LB_FINDSTRINGEXACT 	= 418;
	const LB_SETCURSEL 			= 390;
	const TCM_SETCURSEL 		= 4876;
	const WM_SETTEXT 			= 12;
	
	const GWL_STYLE 			= -16;
	const SC_CLOSE				= 61536;
	const WS_MAXIMIZEBOX 		= 0x10000;
	const WS_MINIMIZEBOX		= 0x20000;
	const WS_SYSMENU			= 0x80000;
	const WS_CLIPCHILDREN 		= 0x02000000;
	const WS_CLIPSIBLINGS 		= 0x04000000;
	
	const SWP_NOZORDER = 0x4;
	const SWP_NOMOVE = 0x2;
	const SWP_NOSIZE = 0x1;
	const SWP_NOACTIVATE = 0x0010;
	const SWP_NOCOPYBITS = 0x0100;
	const SWP_NOOWNERZORDER = 0x0200;
	const SWP_NOREDRAW	= 0x0008;
	
	const GW_HWNDFIRST = 0;
	const GW_HWNDLAST = 1;
	const GW_HWNDNEXT = 2;
	const GW_HWNDPREV = 3;
	const GW_OWNER = 4;
	const GW_CHILD = 5;	
	
	public function __construct()  { 
		parent::__construct('USER');
	}
	
	public function getHwnd($obj) { 
		if ($obj instanceof WBComponent) { 
			return $obj->getHwnd()->hwnd;
		} 
		
		return $obj;
	}

	public function SetCapture($obj) { 
		
		$hwnd = $this->getHwnd($obj);

		$this->call('SetCapture', array($hwnd));
	}

	public function ReleaseCapture($obj) { 
		$this->call('ReleaseCapture');
	}
	
	public function SetMyCursor($obj, $param) { 
		$cursor = $this->call('LoadCursor', array(0, $param));
		$this->call('SetCursor', array($cursor));
	} 
	
	public function MoveWindow($obj, $x, $y, $width, $height) { 
		
		$hwnd = $this->getHwnd($obj);

		$this->call('MoveWindow', array($hwnd, $x, $y, $width, $height, 1));
	}
	
	public function ShowWindow($obj, $options = self::SW_SHOW) { 
		
		$hwnd = $this->getHwnd($obj);
		
		$this->call('ShowWindow', array($hwnd, $options));		
	}
	
	public function GetWindowStyle($obj) { 
		
		$hwnd = $this->getHwnd($obj);
		
		return $this->call('GetWindowLong', array($hwnd, self::GWL_STYLE));
	}
	
	public function SetWindowStyle($obj, $style) { 
		
		$hwnd = $this->getHwnd($obj);
		
		return $this->call('SetWindowLong', array($hwnd, self::GWL_STYLE, $style));
	}	
	
	public function AddWindowStyle($obj, $style) { 
		$this->SetWindowStyle($obj, $this->GetWindowStyle($obj) | $style);
	}
	
	public function DeleteWindowStyle($obj, $style) { 
		$this->SetWindowStyle($obj, $this->GetWindowStyle($obj) & ~$style);
	}	
	
	public function EnableCloseButton($obj, $enable) { 
		
		$hwnd = $this->getHwnd($obj);
		
		$sysmenu = $this->call('GetSystemMenu', array($hwnd, false));
		
		return $this->call('EnableMenuItem', array($sysmenu, self::SC_CLOSE, !$enable));
	}
	
	public function SetParent($obj, $parent) { 
		$hwnd = $this->getHwnd($obj);
		$parentHwnd = $this->getHwnd($parent);
		
		return $this->call('SetParent', array($hwnd, $parentHwnd));
	}
	
	public function SetWindowPos($obj, $x = 0, $y = 0, $width = 0, $height = 0, $zorder = 0, $style = 0) { 
		$hwnd = $this->getHwnd($obj);
		
		return $this->call('SetWindowPos', array($hwnd, $zorder, $x, $y, $width, $height,$style));
	}
	
	public function SetWindowSize($obj, $width, $height) { 
		return $this->SetWindowPos($obj, 0, 0, $width, $height, 0, self::SWP_NOZORDER | self::SWP_NOMOVE);
	}

	public function SetWindowMove($obj, $x, $y) { 
		return $this->SetWindowPos($obj, $x, $y, 0, 0, 0, self::SWP_NOZORDER | self::SWP_NOSIZE);
	}	
	
	public function GetWindow($obj, $command) {
		$hwnd = $this->getHwnd($obj);		 
		return $this->call('GetWindow', array($hwnd, $command));
	}
	
	public function GetWindowList($obj) { 
		$hwnd = $this->getHwnd($obj);
		$arr = array();
		
		$first = $this->GetWindow($hwnd, self::GW_CHILD);
		
		if (!$first)
			return $arr;
			
		$arr[] = $first;

		while(true) { 
			$second = $this->GetWindow($first, self::GW_HWNDNEXT);

			if (!$second) break;
				
			$arr[] = $second;
			$first = $second;
		}
		
		return $arr;
	}
	
}


?>